package com.zzu.gw;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.zzu.R;

public class ListGW extends Activity {
	private final int WC = ViewGroup.LayoutParams.WRAP_CONTENT;  
    private final int FP = ViewGroup.LayoutParams.FILL_PARENT;  
    static String uri="";
    static  ArrayList array=null;
    ListView lv;
    SimpleAdapter adapter;
    GetListHandler handler=null;
    private static Object o=new Object();
	ProgressDialog progressDialog;
	
	class GetListThread extends Thread {
		@Override
		public void run() {
			pain();
		}
	}
	public void pain(){
		array=(ArrayList) getData();
		Message msg = handler.obtainMessage();
		handler.sendMessage(msg);
	}
	class GetListHandler extends Handler {
		@Override
		public void handleMessage(Message msg) {
				final ArrayList list= ListGW.array;
				adapter = new SimpleAdapter(ListGW.this,list,R.layout.vlist,
						new String[]{"title","info","img"},
						new int[]{R.id.title,R.id.info,R.id.img});
				lv=(ListView)ListGW.this.findViewById(R.id.listview);
				lv.setAdapter(adapter);
				 
				lv.setOnItemClickListener(new OnItemClickListener() { 
			    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) { 
			                                                System.out.println("click index:"+arg2); 
			                                           
			                                                Map<String,Object> map=(HashMap)list.get(arg2);
			                                            
			                                                String url=map.get("url").toString();
			                                                Intent intent = new Intent();

			                                    			Bundle bundle = new Bundle();

			                                    			bundle.putString("url",url);

			                                    			intent.setClass(ListGW.this,GwViewActivity.class);

			                                    			intent.putExtras(bundle);

			                                    			startActivity(intent);
			                            						
			        }

				 
			        }  );
				
			
			progressDialog.dismiss();// 关闭进度条
		}
	}
     @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.xinwenzhanshi);
        
        array=null;
        /// getActionBar().setDisplayHomeAsUpEnabled(true);
        
        Bundle bundle = new Bundle();

		bundle = this.getIntent().getExtras();
		 uri="http://10.0.65.228:8081/server/3g/dbgw/imb/searchGW.jsp?pageSize=6&G3PHONE=G3PHONE&CZRY_DM=1207022150060001&ZT=";
		   
		String ZT = bundle.getString("ZT");
		uri+= ZT;
		System.out.println(ZT);
		Button tiJ=(Button)this.findViewById(R.id.button111);
		
		tiJ.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				finish();
			}
		});
		///server/3g/dbgw/imb/searchGW.jsp 1207022150060001   pageSize=6&ZT=DB&G3PHONE=G3PHONE
		
		
		
		progressDialog = new ProgressDialog(ListGW.this);// 生成一个进度条
		progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		progressDialog.setTitle("请稍等");
		progressDialog.setMessage("正在读取数据中!");
		handler = new GetListHandler();
		progressDialog.show();
		new GetListThread().start();
		
		
		
		
		
	}

	private List getData() {
		List list = new ArrayList();
		
		HttpPost request = new HttpPost(uri);
		JSONObject param = new JSONObject();
		try {
			/*param.put("userId", userN.getText().toString());*/
			param.put("LOG_CZRY_DM", "1207022150060001");
			StringEntity se = new StringEntity(param.toString());
			request.setEntity(se);
			HttpResponse httpResponse = new DefaultHttpClient()
					.execute(request);
			String retSrc = EntityUtils.toString(httpResponse
					.getEntity());
			 
			System.out.println(retSrc);
			JSONArray result = new JSONArray(retSrc);
			
			for(int i=0;i<result.length();i++){
				JSONObject o=result.getJSONObject(i);
				Map map=new HashMap();
				
				System.out.println(o.getString("TITLE"));
				map.put("title", o.getString("TITLE"));
				map.put("url", "http://www.baidu.com");
				map.put("info", o.getString("LRR_DM")+"    "+o.getString("LRRQ"));
				map.put("img", R.drawable.star);
				list.add(map);
			}
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	

   /* @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.id.activity_list_gw, menu);
        return true;
    }

    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                NavUtils.navigateUpFromSameTask(this);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
*/
}
